package com.bookshop.logic;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * 通过本机ip向网站获取当地地址。暂时只支持国内。。。
 * @author cqq
 */
public class AddressFinder {
	private String[][] mAddress;
	private int mProvinceIndex;
	private int mCityIndex;

	public AddressFinder() {
		setAddress();
	}

	public String[] getProvince() {
		if (mAddress == null) {
			setAddress();
		}
		String[] province = new String[mAddress.length];
		for (int i = 0; i < province.length; i++) {
			province[i] = mAddress[i][0];
		}
		return province;
	}
	
	public String[] getCity(int pProvinceIndex) {
		int length = mAddress[pProvinceIndex].length - 1;
		String[] cities = new String[length];
		System.arraycopy(mAddress[pProvinceIndex], 1, cities, 0, length);
		return cities;
	}
	
	public int[] getLocalIndex(){
		setLocalAddress();
		return new int[] {mProvinceIndex, mCityIndex};
	}
	
	/**
	 * 完成当地的设置;
	 * {@link #mProvinceIndex}
	 * {@link #mCityIndex}
	 */
	private void setLocalAddress() {
		String addressFromNet = getAddress();
		if (addressFromNet == null) {
			return;
		}
		String[] province = getProvince();
		for (int i = 0; i < province.length; i++) {
			if (addressFromNet.contains(province[i])) {
				mProvinceIndex = i;
				String[] city = getCity(i);
				for (int j = 0; j < city.length; j++) {
					if (addressFromNet.contains(city[j])) {
						mCityIndex = j;
						break;
					}
				}
				break;
			}
		}
	}

	private void setAddress() {
		try {
			FileInputStream fis = new FileInputStream("addr/address.txt");
			Scanner scanner = new Scanner(new InputStreamReader(fis, "utf-8"));
			StringBuilder sb = new StringBuilder();
			while(scanner.hasNextLine()) {
				String line = scanner.nextLine();
				if (!line.equals("")) {
					sb.append(line + ",");
				} else {
					sb.append(';');
				}
			}
			scanner.close();
			String [] buf = sb.toString().split(";");
			int size = buf.length;
			mAddress = new String[size][];
			for(int i = 0; i < buf.length; i++) {
				mAddress[i] = buf[i].split(",");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	private static String getAddress() {
		String address = null;
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress().toString();

			String url = "http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2";
			InputStream in = WebService.getWebSource(url);
			Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(in, "gb2312")));
			while (sc.hasNextLine()) {
				String line = sc.nextLine();
				if (line.contains("本站主数据：")) {
					int index = line.indexOf('：');
					int end = line.indexOf(' ', index);
					sc.close();
					address = line.substring(index + 1, end);
					break;
				}
			}
		} catch (IOException e) {
			// DO-NOTHING.
		}
		return address;
	}
}
